<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;

class Notice extends Api{
    
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    
    public function _initialize()
    {
        parent::_initialize();
    }
    
    /**
     * 公告报名
     */
    public function enlist(){
        $uid = $this->request->post('u');
//         $notice = $this->request->post('n');
        
        if (!$uid)
        {
            $this->error(__('Invalid parameters'));
        }
        
        $user = User::get($uid);
        $user->is_enlist = 1;
        $res = $user->save();
//         $res = model('Enlist')->save(['uid'=>$uid, 'notice_id'=>$notice]);
        
        if($res){
            $this->success(__('Success'));
        }else{
            $this->error(__('Error'));
        }
    }
    
    /**
     * 获取最新一条公告
     */
    public function get(){
        $uid = $this->request->post('u');
        
        if (!$uid)
        {
            $this->error(__('Invalid parameters'));
        }
        
        $res = model('Notice')->field('id,title,content')->where(['type'=>0, 'is_del'=>0])->order('createtime desc')->find();
        
        if (!$res) {
            $this->error('没有找到公告！');
        }
        
        $user = User::get($uid);
//         $enlist = model('Enlist')->where(['uid'=>$uid, 'notice_id'=>$res['id']])->find();
        
        $res['enlist'] = $user->is_enlist;
        
        $this->success(__('Success'), $res);
    }
    
    /**
     * 获取最新一条首页内容
     */
    public function show() {
        $res = model('Notice')->field('id,title,content')->where(['type'=>1, 'is_del'=>0])->order('createtime desc')->find();
        
        if (!$res) {
            $this->error('没有找到公告！');
        }
        
        $this->success(__('Success'), $res);
    }
    
}